/**
 * Created by 功振 on 2016/2/2.
 */
angular.module('NatureCloud').controller('createDockerController', ["$scope", "$cookies", "$mdDialog","dockerTest","dockerShow","buildStart", "projectCodeInfoService", "params",
    function ($scope, $cookies,$mdDialog,dockerTest,dockerShow, buildStart, projectCodeInfoService, params) {
        var user = $cookies.get("username");
        $scope.tags_loading = true;
        //default value is 'master'
        $scope.sub = {};
        $scope.sub.version = "master";
        $scope.type = params.type;
        $scope.editorOptions = {
            lineWrapping : false,
            autoRefresh:true,
            lineNumbers: true,
            theme:'monokai',
            model: 'yaml'
        };
        $scope.dockerfile ='';
        var tem=function () {
            var user=$cookies.get("username");
            dockerShow(user,params).then(function (r) {
                setTimeout(function(){
                    if(r.data){
                        $scope.dockerfile ='';
                    }else{
                    $scope.dockerfile =r ;
                    }},300)
                return r;
            });
        };
        tem();
        $scope.buildInfo=params;


        /*
         *   not support get branch in node.js side, so add default 'master ' branch in frontend
         * */

        if(params.idTagMap.hasOwnProperty(params.id)){
            $scope.versionOptions = params.idTagMap[params.id];
            if($scope.versionOptions.indexOf('master') === -1)
                $scope.versionOptions.push('master');
            $scope.sub.version = $scope.versionOptions[$scope.versionOptions.length - 1];
            $scope.tags_loading = false;
        }else{

            if(params.type === 'git' || params.type === 'svn'){

            }else{
                projectCodeInfoService(user, params.type, params.full_name).then(function (response) {
                    if(response.data instanceof Array){
                        params.idTagMap[params.id] = response.data;
                        $scope.versionOptions = response.data;
                        if($scope.versionOptions.indexOf('master') === -1)
                            $scope.versionOptions.push('master');
                        $scope.sub.version = $scope.versionOptions[$scope.versionOptions.length - 1];
                        $scope.tags_loading = false;
                        console.log("Code Tag list get succeeded");
                    }else if(response.status === 200 && response.data === 'Forbidden'){
                        //alertify.alert("build operate error","Forbidden to get Code Tag list");
                        //$uibModalInstance.dismiss('cancel');
                    }
                    else{
                        window.location.href = response.url;
                    }

                }, function (response) {
                    // alert fail
                    // alertify.alert("build operate error","Code Tag list get failed");
                });
            }
        }
        $scope.test= function () {
            if($scope.dockerfile!==null) {
                dockerTest($scope.dockerfile).then(function (r) {
                    alertify.success(r);
                },function (r) {
                    alertify.success(r);
                });
            }else{
                alertify.success("Dockerfile为空");
            }
        }
        $scope.ok = function () {
            var user = $cookies.get("username");
            if($scope.sub.version !== undefined){
                buildStart(user, params, $scope.sub.version,$scope.sub.branchtype,$scope.dockerfile).then(function (r) {
                    // alert success
                    alertify.success("Dockerfile update successed");
                    //console.log(r + "build start " + params.id + " " + $scope.version + " succeeded");
                    $mdDialog.cancel();
                }, function (r) {
                    // alert fail
                    //console.log(r + "build start failed");
                    alertify.success("Dockerfile update failed");
                    $mdDialog.hide();
                });
            }
        };
        $scope.cancel=function(){
            $mdDialog.cancel();
        };
        $scope.hide=function(){
            $mdDialog.hide();
        }
    }]);